tree grammar Anatree;

// fuente: http://www.antlr.org/wiki/display/ANTLR3/Tree+pattern+matching

options {
  language = Java;
  output = AST;
  tokenVocab = Anasint;
  ASTLabelType = Arbolito;
  backtrack = true;
  filter = true;
}

/*@members {
  float result;
}*/

instrucciones
: ^(INSTRS (result=expr {System.out.println("Resultado: " + result);} )*);


expr returns [float result=0]
: n=NUMERO {$expr.result = Float.parseFloat(n.getText());}
| ^(PLUS izq=expr der=expr) {$expr.result = $izq.result + $der.result;}
| ^(MINUS izq=expr der=expr) {$expr.result = $izq.result - $der.result;}
| ^(ASTERISK izq=expr der=expr) {$expr.result = $izq.result * $der.result;}
| ^(DIV izq=expr der=expr);


  catch [ArithmeticException ae] {
    System.out.println("Error");
    $expr.result = 0;
  }